<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题关键词搜索"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ID、名称" prop="userParam">
        <el-input
          v-model="queryParams.userParam"
          placeholder="请输入用户ID、名称搜索"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论时间">
        <el-date-picker
          v-model="dateRange"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="handleDateChange">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <el-select v-model="queryParams.categoryId">
          <el-option
            v-for="item in classifyData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="mb8" style="display: flex;align-items: center;justify-content: space-between">
      <el-col class="tag-group">
        <el-tag
          v-for="(item, index) in tagItems"
          :key="item.label"
          :effect="tagIndex === index ? 'dark' : 'plain'"
          style="margin-right: 10px"
          @click="handleClick(index)">
          {{ item.label }}
        </el-tag>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="default"
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-setting"
          @click="handleSet"
          style="margin-left: 10px;"
        >分类设置</el-button>
      </el-col>
    </div>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" prop="id" width="80" align="center" sortable />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="分类" align="center" prop="categoryName" />
      <el-table-column label="内容" align="center" prop="cover">
        <template slot-scope="scope" v-if="scope.row.cover">
          <el-image :src="scope.row.cover" @click="handleView(scope.row.content)" style="width: 50px;height: 50px" fit="cover"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="点赞" align="center" prop="likeNum" />
      <el-table-column label="收藏" align="center" prop="collectNum" />
      <el-table-column label="使用" align="center" prop="useNum">
        <template slot-scope="scope">
          <span>{{ scope.row.useNum ? scope.row.useNum : 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评论" align="center" prop="commentNum">
        <template slot-scope="scope">
          <el-button type="text" @click="handleComment(scope.row)">{{ scope.row.commentNum }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="推荐用户ID" align="center" prop="userId" />
      <el-table-column label="推荐用户名" align="center" prop="username" />
      <el-table-column label="推荐时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="checkStatus">
        <template slot-scope="scope">
          <span>{{ scope.row.checkStatus === 1 ? '待审核' : scope.row.checkStatus === 2 ? '已通过' : '未通过' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.checkStatus === 1 || scope.row.checkStatus === 3"
            @click="handleCheck(scope.row, 2)"
          >通过审核</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.checkStatus === 1"
            @click="handleCheck(scope.row, 3)"
          >驳回</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.checkStatus === 2"
            @click="handleOff(scope.row, 3)"
          >下架</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            style="color: #f00"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员管理对话框 -->
    <el-dialog title="评论" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false">
      <div style="text-align: center;">
        <span>{{ commentTotal }}条评论</span>
      </div>
      <div v-for="(item, index) in templateCommentData" :key="index">
        <div style="display: flex;align-items: center;justify-content: space-between;">
          <div style="display: flex;">
            <el-avatar :src="item.headUrl"></el-avatar>
            <div style="margin-left: 10px;">
              <div style="margin-bottom: 5px">
                <span>{{ item.username }}</span>
                <span v-if="item.author" style="margin-left: 10px;background: #f00;color: #fff;border-radius: 5px;padding: 2px 5px;">作者</span>
              </div>
              <div>{{ item.content }}</div>
            </div>
          </div>
          <div style="text-align: right">
            <div>{{ item.createDate }}</div>
            <el-button type="text">删除</el-button>
          </div>
        </div>
      </div>
      <pagination
        v-show="commentTotal>0"
        :total="commentTotal"
        :page.sync="commentParams.pageNum"
        :limit.sync="commentParams.pageSize"
        @pagination="getCommentList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>

    <!--内容查看弹窗-->
    <el-dialog title="内容查看" :visible.sync="contentOpen" width="40%" append-to-body :close-on-click-modal="false">
      <video :src="videoUrl" controls style="width: 100%;height: 400px;"></video>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="contentOpen = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getRecommendTemplateList,
    getRecommendTemplateCount,
    checkRecommendTemplate,
    offlineRecommendTemplate,
    removeRecommendTemplate,
    getClassifylist,
    getCommentListById
  } from "@/api/shuiyin/shuiyin";

  export default {
    name: "RecommendTemplateManage",
    data() {
      return {
        // 位置数据
        tagItems: [
          {
            value: 0,
            label: '全部'
          }, {
            value: 1,
            label: '已通过'
          }, {
            value: 2,
            label: '待审核'
          }, {
            value: 3,
            label: '未通过'
          }
        ],
        // 位置下标
        tagIndex: 0,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 租户管理表格数据
        list: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          type: 1,
          categoryId: '',
          checkStatus: null,
          recommendBeginDate: null,
          recommendEndDate: null,
          title: '',
          userParam: '',
          pageNum: 1,
          pageSize: 10,
        },
        content: {},
        // 分类数据
        classifyData: [],
        // 模板传参
        commentParams: {
          id: null,
          pageNum: 1,
          pageSize: 10
        },
        // 模板评论列表数据
        templateCommentData: [],
        // 模板评论数量
        commentTotal: 3,
        videoUrl: '',
        contentOpen: false
      };
    },
    created() {
      this.getList();
      this.getSetInfo();
      this.getClassify();
    },
    methods: {
      /** 查询租户管理列表 */
      getList() {
        this.loading = true;
        getRecommendTemplateList(this.queryParams).then(response => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getClassify() {
        getClassifylist({type: 1, pageNum: 1,pageSize: 1000}).then(response => {
          this.classifyData = response.rows;
        })
      },
      /** 内容查看操作 */
      handleView(url) {
        if (!url) {
          return
        }
        this.videoUrl = url;
        this.contentOpen = true;
      },
      // 日期时间选择组件确定事件
      handleDateChange(event) {
        this.queryParams.recommendBeginDate = event[0];
        this.queryParams.recommendEndDate = event[1];
      },
      getSetInfo() {
        getRecommendTemplateCount(1).then(response => {
          if (response.data) {
            const { data } = response;
            this.tagItems[1].label = `已通过(${data.checkSuccessNum})`;
            this.tagItems[2].label = `待审核(${data.preCheckNum})`;
            this.tagItems[3].label = `未通过(${data.checkFailNum})`;
          }
        })
      },
      // tag标签点击事件
      handleClick(index) {
        this.tagIndex = index;
        this.queryParams.checkStatus = index;
        this.queryParams.pageNum = 1;
        this.getList();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.commentBeginDate = null;
        this.queryParams.commentEndDate = null;
        this.dateRange = [];
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 审核按钮操作 */
      handleCheck(row, status) {
        // checkStatus	2.通过审核 3.驳回
        this.$modal.confirm(`是否确认${status === 2 ? '审核通过' : '驳回'}所选数据项？`).then(function() {
          return checkRecommendTemplate({
            id: row.id,
            checkStatus: status
          });
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess(`${status === 2 ? '审核通过' : '驳回'}成功`);
        }).catch(() => {});
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除所选数据项？').then(function() {
          return removeRecommendTemplate(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 下架按钮操作 */
      handleOff(row) {
        const id = row.id;
        this.$modal.confirm('是否确认下架所选数据项？').then(function() {
          return offlineRecommendTemplate(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("下架成功");
        }).catch(() => {});
      },
      /** 分类设置 */
      handleSet() {
        this.$router.push("/classifySet/classify/1");
      },
      /** 查看评论数据 */
      handleComment(row) {
        this.commentParams.id = row.id;
        this.getCommentList();
        this.open = true;
      },
      getCommentList() {
        getCommentListById(this.commentParams.id, this.commentParams).then(response => {
          this.templateCommentData = response.rows;
          this.commentTotal = response.total;
        })
      }
    }
  };
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 90px;
    height: 90px;
    line-height: 90px;
    text-align: center;
  }
  .avatar {
    width: 90px;
    height: 90px;
    display: block;
  }
</style>
